<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/16 0016
 * Time: 10:02
 */

namespace app\home\controller;


use app\model\model\Ad;
use app\model\model\Label;
use app\model\model\Seo;
use think\Request;

class Tags extends Base
{
    //实例化
    protected $_Seo;
    protected $_Ad;
    protected $_Label;
    protected $_News;
    protected $_Video;
    protected $_Product;
    public function __construct(Request $request = null){
        parent::__construct($request);
        $this->_Ad = new Ad();
        $this->_Seo = new Seo();
        $this->_Label = new Label();
        $this->_News = new \app\model\model\News();
        $this->_Video = new \app\model\model\Video();
        $this->_Product = new \app\model\model\Product();
    }
    public function index(){
        //SEO
        $seo = $this->_Seo->getOne(['id'=>1]);
        $this->assign('seo',$seo);
        //banner
        $banner = $this->_Ad->getListHome(['type_id'=>20,'flag'=>1]);
        $this->assign('banner',$banner);
        //标签列表
        $tags = $this->_Label->getListSelect();
        $this->assign('tags',$tags);

        if(!input('name')){
            $id = input('id');
            $newslist = $this->_News->getListSelect(['classify'=>1,'flag'=>1,'tags'=>$id],true);
            foreach ($newslist as $v){
                $v['uname'] = str_replace(" ","-",$v['name']);
                $v['create_time'] = date('m, Y',$v['create_time']);
            }
            $bloglist = $this->_News->getListSelect(['classify'=>2,'flag'=>1,'tags'=>$id],true);
            foreach ($bloglist as $v){
                $v['uname'] = str_replace(" ","-",$v['name']);
                $v['create_time'] = date('m, Y',$v['create_time']);
            }
            $klist = $this->_News->getListSelect(['classify'=>3,'flag'=>1,'tags'=>$id],true);
            foreach ($klist as $v){
                $v['uname'] = str_replace(" ","-",$v['name']);
                $v['create_time'] = date('m, Y',$v['create_time']);
            }
            $videolist = $this->_Video->getListSelect(['flag'=>1,'tags'=>$id],true);
            foreach ($videolist as $v){
                $v['uname'] = str_replace(" ","-",$v['name']);
                $v['create_time'] = date('m, Y',$v['create_time']);
            }
            $productlist = $this->_Product->getListSelect(['flag'=>1,'label_id'=>$id],true);
            foreach ($productlist as $v){
                $v['uname'] = str_replace(" ","-",$v['name']);
                $v['create_time'] = date('m, Y',$v['create_time']);
                $v['img'] = json_decode($v['img'],true);
                $v['params'] = json_decode($v['params'],true);
            }
            $list = [
                'news'=>$newslist,
                'blog'=>$bloglist,
                'knowledge'=>$klist,
                'video'=>$videolist,
                  'product'=>$productlist
            ];
            $this->assign('type',1);
        }else{
            $id = input('name');
            $newslist = $this->_News->getListSelect(['classify'=>1,'flag'=>1,'name'=>['like',"%$id%"]],true);
            foreach ($newslist as $v){
                $v['uname'] = str_replace(" ","-",$v['name']);
                $v['create_time'] = date('m, Y',$v['create_time']);
            }
            $bloglist = $this->_News->getListSelect(['classify'=>2,'flag'=>1,'name'=>['like',"%$id%"]],true);
            foreach ($bloglist as $v){
                $v['uname'] = str_replace(" ","-",$v['name']);
                $v['create_time'] = date('m, Y',$v['create_time']);
            }
            $klist = $this->_News->getListSelect(['classify'=>3,'flag'=>1,'name'=>['like',"%$id%"]],true);
            foreach ($klist as $v){
                $v['uname'] = str_replace(" ","-",$v['name']);
                $v['create_time'] = date('m, Y',$v['create_time']);
            }
            $videolist = $this->_Video->getListSelect(['flag'=>1,'name'=>['like',"%$id%"]],true);
            foreach ($videolist as $v){
                $v['uname'] = str_replace(" ","-",$v['name']);
                $v['create_time'] = date('m, Y',$v['create_time']);
            }
            $productlist = $this->_Product->getListSelect(['flag'=>1,'name'=>['like',"%$id%"]],true);
            foreach ($productlist as $v){
                $v['uname'] = str_replace(" ","-",$v['name']);
                $v['create_time'] = date('m, Y',$v['create_time']);
                $v['img'] = json_decode($v['img'],true);
                $v['params'] = json_decode($v['params'],true);
            }
            $list = [
                'news'=>$newslist,
                'blog'=>$bloglist,
                'knowledge'=>$klist,
                'video'=>$videolist,
                'product'=>$productlist
            ];
            $this->assign('type',2);
        }
        if($list['news']==false&&$list['blog']==false&&$list['knowledge']==false&&$list['video']==false&&$list['product']==false){
            $list = false;
        }
        $this->assign('list',$list);
        return $this->fetch();
    }
}